package com.fyzk.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.activity.app.ActivityTaskManager;
import com.fyzk.base.BaseFragment;
import com.fyzk.activity.lottery.recommend_order.CertificationGodActivity;
import com.fyzk.activity.lottery.recommend_order.GeneralGodActivity;
import com.fyzk.activity.lottery.recommend_order.MyRecommendOrderActivity;
import com.fyzk.activity.lottery.recommend_order.MyRecommendOrderBean;
import com.fyzk.activity.lottery.recommend_order.PowerListActivity;
import com.fyzk.activity.lottery.recommend_order.RecommendOrderHallAdapter;
import com.fyzk.activity.lottery.recommend_order.RewardListActivity;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.net.Commands;
import com.fyzk.view.RefreshListView;
import com.fyzk.view.TopTitleView;
import com.fyzk.view.ViewCallBack;
import com.tyjc.lottery.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * Created by qinkang on 2018/5/27.
 */
public class RecommendOrderFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener, RefreshListView.OnRefreshListener {
    private View mainView;

    private List<MyRecommendOrderBean> infos = new ArrayList<>();

    private TopTitleView topView;
    private TextView powerList_tv;          // 实力榜
    private TextView generalGod_tv;         // 普通大神
    private TextView certificationGod_tv;   // 认证大神
    private TextView rewardList_tv;         // 奖励榜
    private RadioButton jczq_rb;
    private RadioButton jclq_rb;
    private RefreshListView listView;
    private RecommendOrderHallAdapter adapter;
    private int type = 1;   // 1为竞彩足球、2为竞彩篮球、3不区分竞彩足球和竞彩篮球
    private int page = 0;
    private int count = 20;
    private View headView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mainView = inflater.inflate(R.layout.ac_recommendorder, null);
        initView();
        findNewRecommendOrders(true);
        return mainView;
    }

    private void initView() {
        topView = mainView.findViewById(R.id.recommendorder_topview);
        listView = mainView.findViewById(R.id.recommendorder_listview);

        headView = LayoutInflater.from(getContext()).inflate(R.layout.recommedn_head_view, null);
        powerList_tv = headView.findViewById(R.id.powerlist_tv);
        generalGod_tv = headView.findViewById(R.id.generalgod_tv);
        certificationGod_tv = headView.findViewById(R.id.certificationgod_tv);
        rewardList_tv = headView.findViewById(R.id.rewardlist_tv);
        jczq_rb = headView.findViewById(R.id.recommendorder_rb_jz);
        jclq_rb = headView.findViewById(R.id.recommendorder_rb_jl);

        topView.getRl().setMinimumHeight(150);
        topView.getRl().setPadding(0,50,0,0);

        topView.setOnTitleClickListener(this);

        jczq_rb.setOnCheckedChangeListener(this);
        jclq_rb.setOnCheckedChangeListener(this);

        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.addHeaderView(headView);
        listView.setOnRefreshListener(this);
        adapter = new RecommendOrderHallAdapter(getContext(), infos);
        listView.setAdapter(adapter);

        powerList_tv.setOnClickListener(this);
        generalGod_tv.setOnClickListener(this);
        certificationGod_tv.setOnClickListener(this);
        rewardList_tv.setOnClickListener(this);

    }

    /**
     * 查询晒单大厅 最新推荐
     *
     * @param showProgress
     */
    private void findNewRecommendOrders(boolean showProgress) {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("raceType", type);    // 类型（竞彩足球：0；竞彩篮球：1）
            request_json.put("startLine", page);
            request_json.put("maxLine", count);
            httpPostJson(CommandUtil.FINDNEWRECOMMENDORDERS, Commands.FINDNEWRECOMMENDORDERS, request_json,
                    showProgress);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        List<MyRecommendOrderBean> beans = JSON.parseArray(resultBean.getData(), MyRecommendOrderBean.class);
        if (listView.isLoadingMore) {
            listView.hideFooterView();
            if (beans == null || beans.size() == 0) {
                listView.noMore();
            } else {
                infos.addAll(beans);
            }
        } else {
            infos = beans;
            listView.hideHeaderView();
            if (beans == null || beans.size() == 0) {
                listView.noMore();
            }
        }
        adapter.setInfos(infos);
    }

    @Override
    public void onFailure(int requestCode, String errorCode, String message) {
        if (errorCode.equals("900003") || errorCode.equals("5")) { // 数据为空
            if (!listView.isLoadingMore) { // 下拉刷新或第一次请求
                infos.clear();
            } else {    // 加载更多
            }
        }
        listView.hideHeaderView();
        listView.noMore();
        adapter.setInfos(infos);
    }

    @Override
    public void onClick(View arg0) {
        Intent intent;
        switch (arg0.getId()) {
            case R.id.powerlist_tv:
                // 实力榜
                intent = new Intent(getContext(), PowerListActivity.class);
                startActivity(intent);
                break;
            case R.id.generalgod_tv:
                // 普通大神
                intent = new Intent(getContext(), GeneralGodActivity.class);
                startActivity(intent);
                break;
            case R.id.certificationgod_tv:
                // 认证大神
                intent = new Intent(getContext(), CertificationGodActivity.class);
                startActivity(intent);
                break;
            case R.id.rewardlist_tv:
                // 奖励榜
                intent = new Intent(getContext(), RewardListActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public void onDownPullRefresh() {
        page = 0;
        findNewRecommendOrders(false);
    }

    @Override
    public void onLoadingMore() {
        page += infos.size();
        findNewRecommendOrders(false);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.recommendorder_rb_jz:
                if (isChecked) {
                    type = 1;
                    page = 0;
                    findNewRecommendOrders(true);
                    jczq_rb.setCompoundDrawablesWithIntrinsicBounds(null,null,null,getResources().getDrawable(R.drawable.underline_check));
                }else {
                    jczq_rb.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
                }
                break;
            case R.id.recommendorder_rb_jl:
                if (isChecked) {
                    type = 2;
                    page = 0;
                    findNewRecommendOrders(true);
                    jclq_rb.setCompoundDrawablesWithIntrinsicBounds(null,null,null,getResources().getDrawable(R.drawable.underline_check));
                }else {
                    jclq_rb.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null);
                }
                break;
        }
    }

    @Override
    public void onRightClick() {
        Intent intent;
        if (!MySharedPreferences.getInstance().getLoginStatus()) {
            AppLotteryInfos.clear();
            ViewCallBack.getInstance().countDownObjs.clear();
            ActivityTaskManager.getInstance().closeAllActivity();
            intent = new Intent(getContext(), LoginActivity.class);
            startActivity(intent);
            return;
        }
        intent = new Intent(getContext(), MyRecommendOrderActivity.class);
        startActivity(intent);
    }

    @Override
    public void onLeftClick() {

    }

}
